package com.smedi.ismedi.constructionproject.core.domain.service.impl;

import com.smedi.ismedi.constructionproject.core.domain.aggregatemodel.BidSectionConfig;
import com.smedi.ismedi.constructionproject.core.domain.repository.BidSectionConfigRepository;
import com.smedi.ismedi.constructionproject.core.domain.service.BidSectionConfigDomainService;
import com.smedi.ismedi.constructionproject.core.domain.service.BidSectionConfigOrganizationDomainService;
import com.smedi.ismedi.constructionproject.core.infrastructure.enumerate.BidSectionConfigEnum;
import com.smedi.ismedi.constructionproject.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: xulinjun
 * @Date: 2021-6-22 10:45
 */
@Slf4j
@Service
@AllArgsConstructor
public class BidSectionConfigDomainServiceImpl implements BidSectionConfigDomainService {
    private final BidSectionConfigOrganizationDomainService bidSectionConfigOrganizationDomainService;

    private final BidSectionConfigRepository bidSectionConfigRepository;

    @Override
    public void validate(List<BidSectionConfig> bidSectionConfigs, Long projectId) {
        // 校验默认配置项
        List<String> standards = new ArrayList<String>() {{
            add(BidSectionConfigEnum.OWNER_SITE_MANAGEMENT_ORGANIZATION.getValue());
            add(BidSectionConfigEnum.SUPERVISION_SITE_MANAGEMENT_ORGANIZATION.getValue());
            add(BidSectionConfigEnum.DESIGN_SITE_MANAGEMENT_ORGANIZATION.getValue());
            add(BidSectionConfigEnum.CONSTRUCTION_SITE_MANAGEMENT_ORGANIZATION.getValue());
        }};

        // 校验配置信息上的组织信息
        List<Long> projectOrganizations = new ArrayList<>();
        bidSectionConfigs.stream().forEach(bidSectionConfig -> {
            // 建设单位、设计单位、施工单位、监理单位只允许放一个项目组织
            if (standards.contains(bidSectionConfig.getOrganizationType())) {
                if (bidSectionConfig.getBidSectionConfigOrganizations().size() > 1) {
                    throw LogicException.raise(CustomError.BID_SECTION_CONFIG_TYPE_CAN_ONLY_HAS_ONE_ORG);
                }
            }

            bidSectionConfig.getBidSectionConfigOrganizations().stream().forEach(bidSectionConfigOrganization -> {
                if (bidSectionConfigOrganization.getProjectOrganizationId() != null) {
                    projectOrganizations.add(bidSectionConfigOrganization.getProjectOrganizationId());
                }
            });
        });
        bidSectionConfigOrganizationDomainService.validate(projectOrganizations, projectId);

        Set<String> duplicates = new HashSet<>();
        bidSectionConfigs.stream().forEach(i -> {
            if (standards.contains(i.getOrganizationType())) {
                standards.remove(i.getOrganizationType());
            }
            if (!StringUtils.isEmpty(i.getOrganizationType())) {
                duplicates.add(i.getOrganizationType());
            }
        });
        // 2021.6.28 这段逻辑移除
        /*if (standards.size() > 0) {
            throw LogicException.raise(ErrorEnum.STANDARD_BID_SECTION_CONFIG_MISS);
        }*/
        if (duplicates.size() != bidSectionConfigs.size()) {
            throw LogicException.raise(CustomError.BID_SECTION_CONFIG_TYPE_DUPLICATE);
        }
    }

    @Override
    public void cleanBidSectionConfig(Long bidSectionId) {
        bidSectionConfigRepository.deleteByBidSectionId(bidSectionId);
        bidSectionConfigOrganizationDomainService.cleanByBidSectionId(bidSectionId);
    }

    @Override
    public List<BidSectionConfig> saveBidSectionConfig(List<BidSectionConfig> bidSectionConfigs, Long bidSectionId, Long projectId, boolean clean) {
        List<BidSectionConfig> results = new ArrayList<>();
        if (clean) {
            this.cleanBidSectionConfig(bidSectionId);
        }
        for (BidSectionConfig bidSectionConfig : bidSectionConfigs) {
            bidSectionConfig.setBidSectionId(bidSectionId)
                    .setProjectId(projectId)
                    .setMultiple(bidSectionConfig.getBidSectionConfigOrganizations().size() > 1);
            BidSectionConfig domain = bidSectionConfigRepository.doInsert(bidSectionConfig);

            domain.setBidSectionConfigOrganizations(bidSectionConfigOrganizationDomainService.saveBidSectionConfigOrganization(bidSectionConfig.getBidSectionConfigOrganizations()
                    , domain.getBidSectionId()
                    , projectId
                    , domain.getBidSectionConfigId()));
            results.add(domain);
        }

        return results;
    }
}
